local NameTitleNode = class("NameTitleNode",function()
	return display.newNode()
end)


local NodeConfigs = {
	{size=16,color=cc.c4b(252,239,223,255) ,outlineColor=cc.c4b(14,14,14,255),outlineSize=2}, --nick name
	{size=16,outlineColor=cc.c4b(14,14,14,255),outlineSize=2},	--title name
	{size=16,outlineColor=cc.c4b(14,14,14,255),outlineSize=2},	--guild name
	{size=16,outlineColor=cc.c4b(14,14,14,255),outlineSize=2},  --patrol state
	{size=16,outlineColor=cc.c4b(14,14,14,255),outlineSize=2},  --owner guild name
}

local NodeLinespace = 20

local function newLabel(type)
	local config = NodeConfigs[type]
	local text = ccui.Text:create("", Res.defaultFont, config.size)
	text:setAnchorPoint(0.5,0.0)
	local color = config.color
	if color then
		text:setTextColor(color)
	end
	local outlineColor = config.outlineColor
	local outlineSize = config.outlineSize
	if outlineColor and outlineSize then
		text:enableOutline(outlineColor,outlineSize)
	end
	return text
end

function NameTitleNode:ctor()
	self._contents = {

	}

	self._nodes = {

	}

	self:setCascadeColorEnabled(true)
   	self:setCascadeOpacityEnabled(true)
end

--名字
function NameTitleNode:setName(str)
	if not app:isBattleGuideComp() then
		self:_setContent(1,"")
		return
	end
	self:_setContent(1,str)
end

function NameTitleNode:setNameColor(c)
	self:_setNodeColor(1,c)
end

--称号
function NameTitleNode:setTitle(str)
	self:_setContent(2,str)
end

function NameTitleNode:setTitleColor(c)
	self:_setNodeColor(2,c)
end

function NameTitleNode:setGuildName(str)
	self:_setContent(3,str)
end

function NameTitleNode:setGuildNameColor(c)
	self:_setNodeColor(3, c)
end

function NameTitleNode:showPatrolName(b)
	-- self:_setContent(4,b and L("on_patrol") or "")
	self:_setContent(4, b and L("lua_code_text_452") or "")
end

function NameTitleNode:setOwnerGuildName(str)
	self:_setContent(5,str)
end

function NameTitleNode:setOwnerGuildNameColor(c)
	self:_setNodeColor(5, c)
end

function NameTitleNode:_setContent(i,str)
	local contents = self._contents
	local content = contents[i]
	if contents ~= str then--and string.len(str)>0
		contents[i] = str
		self:_updateContent(i)
	end
end

function NameTitleNode:_setNodeColor(i,color)
	local nodes = self._nodes
	local node = nodes[i]
	if node then
		node:setTextColor(color)
	end
end


function NameTitleNode:_updateContent(i)
	local contents = self._contents
	local nodes = self._nodes

	local content = contents[i]
	local node = nodes[i]
	if content and string.len(content)>0 then
		if not node then
			node = newLabel(i)
			self:addChild(node)
			nodes[i] = node
			self._nodes = nodes
		end
		node:setString(content)
		node:setVisible(true)
		self:_updateLayout()
	else
		if node then
			node:setVisible(false)
			self:_updateLayout()
		end
	end
end

function NameTitleNode:_updateLayout()
	local nodes = self._nodes
	local offset = 0
	for i=1,#NodeConfigs do
		local node = nodes[i]
		if node and node:isVisible() then
			node:setPositionY(offset)
			offset = offset + NodeLinespace
		end
	end
end

function NameTitleNode:getNodeHeight()
	local nodes = self._nodes
	local offset = 0
	for i=1,#NodeConfigs do
		local node = nodes[i]
		if node and node:isVisible() then
			offset = offset + NodeLinespace
		end
	end
	return offset
end

function NameTitleNode:containWorldPoint(x, y)
	local nodes = self._nodes
	local rect, worldPos
	for i=1,#NodeConfigs do
		local node = nodes[i]
		if node and node:isVisible() then
			rect = node:getBoundingBox()
			worldPos = node:getParent():convertToWorldSpaceAR(cc.p(rect.x, rect.y))
			rect.x = worldPos.x
			rect.y = worldPos.y
			if cc.rectContainsPoint(rect, cc.p(x, y)) then
				return true
			end
		end
	end
	return false
end


return NameTitleNode
